package TankFrst;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;

public class Tank {

    int id;//坦克的id

    public static final int XSPREED=5;//定义速度横坐标的速度
    public static final int YSPREED=5;//定义速度纵坐标的速度

    public static final int TANKHIGH=30;//坦克的高度
    public static final int TANKWIGTH=30;//坦克的宽度

    TankClient tc=null;

//    private bloodBar bb=new bloodBar();

    int x,y;//设置当前的位置
    int oldX,oldY;//前一步的坐标
    private boolean good;//坦克敌人和自己：自己为true，敌人为false
    boolean live=true;

    public boolean isGood() {
        return good;
    }
    public void setGood(boolean good) {
        this.good = good;
    }


    private static final Random r=new Random();
    private int step=r.nextInt(12)+3;

    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    private boolean bL=false,bU=false,bR=false,bD=false;//用于判断按下了什么键


     Dir dir=Dir.STOP;//定义坦克最开始的情况为禁止的
     Dir ptDir=Dir.D;//炮筒方向

    public Tank(int x, int y,boolean good) {
        this.x = x;
        this.y = y;
        this.oldX=x;
        this.oldY=y;
        this.good=good;
    }

    public Tank(int x, int y, boolean good, Dir dir, TankClient tc ) {
        this(x,y, good);
        this.tc = tc;
        this.dir=dir;
    }
//画笔画坦克：
    public void draw(Graphics g){
        if (!isLive()) {
            if (!good){
                tc.tanks.remove(this);
            }
            return;
        }
        Color c=g.getColor();
        if (good) g.setColor(Color.red);
        else g.setColor(Color.blue);
        g.fillOval(x,y,TANKWIGTH,TANKHIGH);
        g.drawString("id+"+id,x,y-5);
        g.setColor(c);
        //画子弹
        switch(ptDir){
            case L:
                //向左边
                g.drawLine(x+TANKWIGTH,y+TANKHIGH/2,x,y+TANKHIGH/2);
                break;
            case LU:
                //向左上
                g.drawLine(x+TANKWIGTH/2,y+TANKHIGH/2,x,y);
                break;
            case U:
                //向上
                g.drawLine(x+TANKWIGTH/2,y+TANKHIGH/2,x+TANKWIGTH/2,y);
                break;
            case RU:
                //向右上
                g.drawLine(x+Tank.TANKWIGTH/2,y+TANKHIGH/2,x+TANKWIGTH,y);
                break;
            case R:
                //向右
                g.drawLine(x+TANKWIGTH/2,y+TANKHIGH/2,x+TANKWIGTH,y+TANKHIGH/2);
                break;
            case RD:
                //向右下
                g.drawLine(x+TANKWIGTH/2,y+TANKHIGH/2,x+TANKWIGTH,y+TANKHIGH);
                break;
            case D:
                //向下
                g.drawLine(x+TANKWIGTH/2,y+TANKHIGH/2,x+TANKWIGTH/2,y+TANKHIGH);
                break;
            case LD:
                //向左下
                g.drawLine(x+TANKWIGTH/2,y+TANKHIGH/2,x,y+TANKHIGH);
                break;
        }
        move();//调用移动方向
    }

//方向的判断
    void move(){

        this.oldX=x;
        this.oldY=y;

        switch(dir){
            //向下坐标
            case L:
                x-=XSPREED;
                break;
                //向左上
            case LU:
                x-= XSPREED;
                y-=YSPREED;
                break;
                //向下
            case U:
                y-=YSPREED;
                break;
                //向右下
            case RU:
                x+= XSPREED;
                y-=YSPREED;
                break;
                //向右
            case R:
                x+= XSPREED;
                break;
                //向右下
            case RD:
                x+= XSPREED;
                y+=YSPREED;
                break;
                //向下
            case D:
                y+=YSPREED;
                break;
                //向左下
            case LD:
                x-= XSPREED;
                y+=YSPREED;
                break;
                //停止
            case STOP:
                break;
        }
        if (dir!=Dir.STOP){
            this.ptDir=this.dir;
        }
        //防止坦克开出界面
        if (x<0) x=0;
        if (y<30) y=30;
        if (x+Tank.TANKWIGTH> TankClient.GAME_WEIGHT) x= TankClient.GAME_WEIGHT-Tank.TANKWIGTH;
        if (y+Tank.TANKHIGH> TankClient.GAME_HIGH) y= TankClient.GAME_HIGH-Tank.TANKHIGH;

//        if (!good){
//            Dir[] dirs= Dir.values();//将enum转换成一个数组
//            int rn=r.nextInt(dirs.length);
//            if (step==0) {
//                step=r.nextInt(12)+3;
//                dir = dirs[rn];
//            }
//            step--;
//            if(r.nextInt(40)>38) this.fire();//发射子弹
//        }
    }

    public void stay(){
        x=oldX;
        y=oldY;
    }
//根据监听按键的信息来为下面的方法来判断当前按键的方向
    public void keyPressed(KeyEvent e) {
        int key=e.getKeyCode();
        /*
        A:向左
        S:向下
        D：向右
        W：向上
         */
        switch (key){
            case KeyEvent.VK_A:
                bL=true;
                break;
            case KeyEvent.VK_W:
                bU=true;
                break;
            case KeyEvent.VK_D:
                bR=true;
                break;
            case KeyEvent.VK_S:
                bD=true;
                break;
        }
        locateDirection();
        }
        //判断八个方向的按键
     void locateDirection(){
        Dir oldDir=this.dir;//记录原前的方向，来发送消息
        //来判断自己的方向
        if (bL&&!bU&&!bR&&!bD) dir=Dir.L;
        else if (bL&&bU&&!bR&&!bD) dir=Dir.LU;
        else if (!bL&&bU&&!bR&&!bD) dir=Dir.U;
        else if (bL&&!bU&&!bR&&bD) dir=Dir.LD;
        else if (!bL&&bU&&bR&&!bD) dir=Dir.RU;
        else if (!bL&&!bU&&bR&&!bD) dir=Dir.R;
        else if (!bL&&!bU&&bR&&bD) dir=Dir.RD;
        else if (!bL&&!bU&&!bR&&bD) dir=Dir.D;
        else if (!bL&&!bU&&!bR&&!bD) dir=Dir.STOP;
        if (dir!=oldDir){
            TankMoveMsg msg = new TankMoveMsg(id,x,y,dir,ptDir);
            tc.nc.send(msg);
        }
     }
// STOPSHIP: 2022/4/20
    //监听键盘按下
public void keyReleased(KeyEvent e) {
        //监听松开按键，用于来开火的速度
    int key=e.getKeyCode();
    switch (key){
        case KeyEvent.VK_J:
            fire();
            break;
        case KeyEvent.VK_A:
            bL=false;
            break;
        case KeyEvent.VK_W:
            bU=false;
            break;
        case KeyEvent.VK_D:
            bR=false;
            break;
        case KeyEvent.VK_S:
            bD=false;
            break;
    }
    locateDirection();//设置方向
}

public Missile fire(){
        //设置子弹打人的
        if (!live) return null;
        int x=this.x+Tank.TANKWIGTH/2-Missile.MISSILEWIGTH/2;
        int y=this.y+Tank.TANKHIGH/-Missile.MISSILEHIGH/2;
        Missile m=new Missile(id,x,y,this.good,ptDir,this.tc);
        tc.missiles.add(m);
//发射子弹的时候传递消息
        MissileNewMsg msg=new MissileNewMsg(m);
        tc.nc.send(msg);

        return m;
}

    public Rectangle getRect(){
        //设置矩形块，检测是否相撞
        return new Rectangle(x,y,Tank.TANKWIGTH,Tank.TANKHIGH);
    }


    public boolean collidesWithTanks(List<Tank> tanks){
        //防止坦克的相撞
        for (int i = 0; i < tanks.size(); i++) {
            Tank t=tanks.get(i);
            if (this!=t){
                if(this.live&&t.live&&this.getRect().intersects(t.getRect())){
                    this.stay();
                    t.stay();
                    return true;
                }
            }
        }
        return false;
    }
}

